﻿/*
Copyright (c) 2010 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Windows.Forms;
using TeachMe.Application.Configuration;
using TeachMe.Application.Forms;

namespace TeachMe.Application.Import
{
    public partial class ImportForm : PseudoModalForm
    {
        private enum WizardLevel
        {
            Introduction,
            Source,
            Destination,
            Options
        }

        private WizardLevel _level;
        private WizardPage _currentPage;
        private readonly Root _root;

        public ImportForm(Root root)
        {
            _root = root;
            _level = WizardLevel.Introduction;

            InitializeComponent();

            WindowProportions.Lock(this, _root.MachineSettings);

            AdjustButtons();
            AdjustPages();
        }

        private void ShowPage<T>() where T : WizardPage, new()
        {
            foreach (Control control in Controls)
            {
                if (!(control is WizardPage))
                {
                    continue;
                }

                control.Hide();
            }

            foreach (Control control in Controls)
            {
                if (!(control is T))
                {
                    continue;
                }

                _currentPage = (WizardPage)control;

                control.Show();
                return;
            }

            T page = new T();
            Controls.Add(page);
            page.Dock = DockStyle.Fill;
            page.Changed += OnPageChanged;

            _currentPage = page;
        }

        void OnPageChanged(object sender, EventArgs e)
        {
            ValidateCurrentPage();
        }

        private void ValidateCurrentPage()
        {
            bNext.Enabled = _currentPage.Valid;
        }

        private void AdjustPages()
        {
            switch (_level)
            {
                case WizardLevel.Introduction:
                    ShowPage<Introduction>();
                    break;
                case WizardLevel.Source:
                    ShowPage<Source>();
                    break;
                case WizardLevel.Destination:
                    break;
                case WizardLevel.Options:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            ValidateCurrentPage();
        }

        private void OnNextClick(object sender, EventArgs e)
        {
            switch (_level)
            {
                case WizardLevel.Introduction:
                    _level = WizardLevel.Source;
                    break;
                case WizardLevel.Source:
                    _level = WizardLevel.Destination;
                    break;
                case WizardLevel.Destination:
                    _level = WizardLevel.Options;
                    break;
                case WizardLevel.Options:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            AdjustButtons();
            AdjustPages();
        }

        private void OnBackClick(object sender, EventArgs e)
        {
            switch (_level)
            {
                case WizardLevel.Introduction:
                    break;
                case WizardLevel.Source:
                    _level = WizardLevel.Introduction;
                    break;
                case WizardLevel.Destination:
                    _level = WizardLevel.Source;
                    break;
                case WizardLevel.Options:
                    _level = WizardLevel.Destination;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            AdjustButtons();
            AdjustPages();
        }

        private void AdjustButtons()
        {
            bNext.Enabled = _level != WizardLevel.Options;
            bBack.Enabled = _level != WizardLevel.Introduction;
        }
    }
}
